package com.qqtech.qquan.circle.dao;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Repository;

import com.qqtech.core.common.util.BeanUtil;
import com.qqtech.core.frame.dao.BaseDaoImpl;
import com.qqtech.qquan.circle.model.CircleInfo;
import com.qqtech.qquan.circle.model.CircleInfoQuery;
import com.qqtech.qquan.circle.model.CircleInfoVo;

@Repository
public class CircleInfoDaoImpl extends BaseDaoImpl<CircleInfo> implements CircleInfoDao {
	public static final Logger log = LoggerFactory.getLogger(CircleInfoDaoImpl.class);

	@Override
	public int increasePv(int circleId, int pv) {
		CircleInfo cInfo = new CircleInfo();
		cInfo.setId(circleId);
		cInfo.setPv(pv);
		return sqlSessionTemplate.update(getSqlName("increasePv"), cInfo);
	}

	@Override
	public int increaseMemberCount(int circleId, int memberCount) {
		CircleInfo cInfo = new CircleInfo();
		cInfo.setId(circleId);
		cInfo.setMemberCount(memberCount);
		return sqlSessionTemplate.update(getSqlName("increaseMemberCount"), cInfo);
	}

	@Override
	public int reduceMemberCount(int circleId, int memberCount) {
		CircleInfo cInfo = new CircleInfo();
		cInfo.setId(circleId);
		cInfo.setMemberCount(memberCount);
		return sqlSessionTemplate.update(getSqlName("reduceMemberCount"), cInfo);
	}

	@Override
	public int increaseTopicCount(int circleId, int topicCount) {
		CircleInfo cInfo = new CircleInfo();
		cInfo.setId(circleId);
		cInfo.setTopicCount(topicCount);
		return sqlSessionTemplate.update(getSqlName("increaseTopicCount"), cInfo);
	}

	@Override
	public int reduceTopicCount(int circleId, int topicCount) {
		CircleInfo cInfo = new CircleInfo();
		cInfo.setId(circleId);
		cInfo.setTopicCount(topicCount);
		return sqlSessionTemplate.update(getSqlName("reduceTopicCount"), cInfo);
	}

	@Override
	public int queryCountBaseInfo(CircleInfoQuery query) {
		return sqlSessionTemplate.selectOne(getSqlName("selectCountForBaseInfo"), BeanUtil.toMap(query));
	}
	
	@Override
	public Page<CircleInfoVo> queryListBaseInfo(CircleInfoQuery query, Pageable pageable) {
		List<CircleInfoVo> contentList = sqlSessionTemplate.selectList(getSqlName("selectForBaseInfo"), getParams(query, pageable));
		return new PageImpl<CircleInfoVo>(contentList, pageable, this.queryCountBaseInfo(query));
	}
	
}
